<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../js/vue.js"></script>
    <script src="/js/vue.js"></script>
    <script src="/js/axios.min.js"></script>
    <script src="/element-ui/lib/index.js"></script>
    <link rel="stylesheet" href="/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
    <el-form :inline="true"  class="demo-form-inline">
        <el-form-item label="选择字典类型">
            <el-select v-model="typeCode" placeholder="选择字典类型">
                <el-option label="--请选择--" value=""></el-option>
                <el-option v-for="item in types" :label="item.name" :value="item.code"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="initData">查询</el-button>
            <el-button type="success" @click="openDialog()">添加</el-button>
            <el-button type="danger" @click="del()">批量删除</el-button>
        </el-form-item>
    </el-form>

    <el-table
            :data="values"
            border
            style="width: 100%"
            @selection-change="handleSelectionChange">
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                fixed
                prop="value"
                label="字典值"
                width="150">
        </el-table-column>
        <el-table-column
                prop="text"
                label="字典值文本"
                width="120">
        </el-table-column>
        <el-table-column
                prop="orderNo"
                label="排序"
                width="120">
        </el-table-column>
        <el-table-column
                prop="type.name"
                label="字典类型名称"
                width="120">
        </el-table-column>
        <el-table-column
                prop="typeCode"
                label="类型编码"
                width="300">
        </el-table-column>
        <el-table-column
                label="操作"
                width="100">
            <template slot-scope="scope">
                <el-button @click="del(scope.row.id)" type="text" size="small">删除</el-button>
                <el-button type="text" size="small" @click="openDialog(scope.row)">编辑</el-button>
            </template>
        </el-table-column>
    </el-table>

    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageNum"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
    </el-pagination>
<!--添加编辑弹框-->
    <el-dialog title="字典值维护" :visible.sync="dialogFormVisible">
        <el-form :model="val">
            <el-form-item label="选择字典类型">
                <el-select v-model="val.typeCode" placeholder="选择字典类型">
                    <el-option v-for="item in types" :label="item.name" :value="item.code"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="字典值" :label-width="formLabelWidth">
                <el-input v-model="val.value" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="字典值文本" :label-width="formLabelWidth">
                <el-input v-model="val.text" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="排序" :label-width="formLabelWidth">
                <el-input-number v-model="val.orderNo" :min="1"  label="描述文字"></el-input-number>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveVal">确 定</el-button>
        </div>
    </el-dialog>
</div>

<script>
    new Vue({
        el: "#app",
        data: {
            typeCode:undefined,//搜索条件
            values:[],
            pageNum:1,
            pageSize:5,
            total:undefined,
            types:[] , //所有的字典类型
            dialogFormVisible: false,
            formLabelWidth: '120px',
            val:{//待修改或者添加的字典值对象
                id:undefined,
                value:undefined,
                text:undefined,
                orderNo:1,
                typeCode:undefined
            },
            multipleSelection:[] //选中的行数据
        },
        methods: {
            del(id){//删除
                let idArr=[];
                if (id){//单个删除
                    idArr.push(id)
                }else{//批量删除
                    this.multipleSelection.forEach(item=>{
                        idArr.push(item.id)
                    })
                }

                if (idArr.length==0){
                    this.$message({
                        type:"warning",
                        message:"至少选中一条数据进行删除"
                    });
                    return;
                }

                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {

                    axios.delete("/value?ids="+idArr.join(",")).then(resp=>{
                        let data=resp.data;
                        if (data.code==200){
                            this.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                            //刷新当前表格数据
                            this.initData();
                        }else{
                            this.$message({
                                type: 'error',
                                message: data.msg
                            });
                        }
                    })



                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            handleSelectionChange(val) {//每一行的复选框点击事件
                this.multipleSelection = val;
            },
            openDialog(item) {//打开添加/编辑的弹框
                this.dialogFormVisible=true;
                if (item){//修改
                    this.val={...item}
                }else{//添加
                    this.val.id=undefined;
                    this.val.value=undefined
                    this.val. text=undefined
                    this.val.orderNo=1
                    this.val.typeCode=undefined
                }
            },
            saveVal(){
                axios({
                    method:this.val.id?"put":"post",
                    url:"/value",
                    data:this.val
                }).then(resp=>{
                    let data=resp.data;
                    if (data.code==200){
                        this.$message({
                            type:"success",
                            message:"操作成功"
                        });
                        this.dialogFormVisible=false;
                        //刷新表格数据
                        this.initData();
                    }else{
                        this.$message({
                            type:"error",
                            message:data.msg
                        });
                    }
                })
            },
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.initData()
            },
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.pageNum=val;
                this.initData()
            },
            initData(){//获取表格数据
                axios.get("/value",{params:{
                        pageNumber:this.pageNumber,
                        pageSize:this.pageSize,
                        typeCode:this.typeCode
                    }}).then(resp=>{
                        let data=resp.data;
                        this.values=data.result;
                        this.total=data.count;

                })
            },
            getAllDicType(){//获取所有的字典类型
                axios.get("/type?pageSize=10000").then(resp=>{
                        let data=resp.data;
                        this.types=data.result
                })
            }
        },
        created(){
            this.initData();
            this.getAllDicType();
        }
    })
</script>
</body>
</html>